﻿using System;
namespace Quadrangle;


    public abstract class shape
    {
        public abstract double getarea();
        public abstract void Istrue();
    }

     public class Rectangle : shape
    {
    private double len,wid;
    public Rectangle()
    {
        Istrue();
    }
    public override double getarea()
    {
        return
                this.len * this.wid;
    }

    public override void  Istrue()
    {
            Console.WriteLine("请输入长方形的长：");
            string lenx = Console.ReadLine();
            Console.WriteLine("请输入长方形的宽：");
            string widx = Console.ReadLine();
            if (!double.TryParse(widx, out wid) || !double.TryParse(lenx, out len) || wid < 0 || len < 0 || len < wid)
            {
                Console.WriteLine("输入错误");
            }
    }



    class Square : shape
    {
        public double side;
        public Square()
        {
            Istrue();
        }
        public override void Istrue()
        {
                Console.WriteLine("请输入正方形的边长：");
                string sidex = Console.ReadLine();
                if (!double.TryParse(sidex, out side) || side < 0)
                {
                    Console.WriteLine("输入错误");
                }
        }

        public override double getarea()
        {
            return this.side * this.side;
        }
    }

    class triangle : shape
    {
        public double a, b, c;
        public triangle()
        {
            Istrue();
        }

        public override void Istrue()
        {
                Console.WriteLine("请输入三角形的第一条边：");
                string ax = Console.ReadLine();
                Console.WriteLine("请输入三角形的第二条边：");
                string bx = Console.ReadLine();
                Console.WriteLine("请输入三角形的第三条边：");
                string cx = Console.ReadLine();
                if (!double.TryParse(ax, out a) ||
                    !double.TryParse(bx, out b) ||
                    !double.TryParse(cx, out c) ||
                    a >= b + c ||
                    b >= a + c ||
                    c >= a + b||
                        a<0||
                        b<0||
                        c<0)
                {
                    Console.WriteLine("输入错误");
                }
            
        }

        public override double getarea()
        {
            double m = (a+b+c)/2;
            return Math.Sqrt(m * (m - a) * (m - b) * (m - c));
        }
    }

class JudgeShape
{
    public static shape Creatshape(string name)
    {
        switch (name)
        {
            case "长方形": return new Rectangle();
            case "正方形": return new Square();
            case "三角形": return new triangle();
            default:
                return null;
        }
    }
}

class QuaCountandJudge
{
    public static void Main(string[] argc)
    {
        Console.WriteLine("请输入你想选择的形状：");
        Console.WriteLine("长方形，正方形，三角形");
        string name = Console.ReadLine();
        shape shape = JudgeShape.Creatshape(name);

            if (shape != null)
            {
                Console.WriteLine("面积为：" + shape.getarea());
            }
            else
                Console.WriteLine("输入错误");
    }
}
}
